﻿<?php
class NvngaysController extends AppController {

	var $name = 'Nvngays';
	var $helpers = array('Js','Html','Form','Ctable');
    var $uses=array('Nvngay','Nhanvien','Donvi','Tempnhiemvunv');
    
    var $components = array('Session', 'Cookie');
    
    /** kq dung de luu cong viec cua nhan vien o lan dau tien
     * 
     * @param kehoach       idkehoach can luu cong viec
     * 
     * @return              Tro ve "hien" sau khi nhap xong de ng ta kiem tra lai
     * */
    function kq($kehoach=1){
        
        $kh=$this->Nvngay->Kehoach->find('first',array('conditions'=>array('Kehoach.id'=>$kehoach)));
       
        $nhanviens=$this->Tempnhiemvunv->find('all',array(
                'fields'=>array('id','Nhanvien.manv','Nhanvien.tennv','Tempnhiemvunv.kehoach_id',
                                        'Tempnhiemvunv.nhanvien_id'),
                'order'=>array('Nhanvien.id'=>'asc'),
                'conditions'=>array('kehoach_id'=>$kehoach)));
           
       //print_r($donvis);
      	$this->set(compact('kehoaches','cot','dong','kehoach','nhanviens','idnhanvien','kh'));
        
    }
    
    /** kqnv dung de luu cong viec cua nhan vien o lan dau tien
     * 
     * @param kehoach       idkehoach can luu cong viec
     * @param idnhanvien    dung trong truong hop ca nhan vien tu luu
     * 
     * @return              Tro ve "hien" sau khi nhap xong de ng ta kiem tra lai
     * */
    function kqnv($kehoach=1,$idnhanvien=null){
         $kh=$this->Nvngay->Kehoach->find('first',array('conditions'=>array('Kehoach.id'=>$kehoach)));
         $this->set(compact('kh'));
        
         $nhanviens=$this->Tempnhiemvunv->find('all',array(
                'fields'=>array('id','Nhanvien.manv','Nhanvien.tennv','Tempnhiemvunv.kehoach_id',
                                        'Tempnhiemvunv.nhanvien_id'),
                'order'=>array('Nhanvien.id'=>'asc'),
                'conditions'=>array('kehoach_id'=>$kehoach,'nhanvien_id'=>$idnhanvien)));
            
         
     
       //print_r($donvis);
      	$this->set(compact('kehoaches','cot','dong','kehoach','nhanviens','idnhanvien'));
        
    }
    function luu ($kehoach=1,$idnhanvien=null){
     //   $colnum = $this->data['Nvngay']['socot'];
     
    	$rownum = $this->data['Nvngay']['sodong'];
        
        for($i=0;$i<$rownum;$i++)
            for($j=0;$j<7;$j++){
                
                
                    $record['Nvngay']['kehoach_id']=$kehoach;
                    $record['Nvngay']['nhanvien_id']=$this->data['Nvngay']['nv'][$i];
                    
                /** De phong cac truong hop khong nhap lieu thi tu dong nhap cho truong do
                 * */
                    if($this->data['Nvngay'][$i][$j][0]!=null)
                        $record['Nvngay']['nhiemvu']=$this->data['Nvngay'][$i][$j][0];
                    else
                        $record['Nvngay']['nhiemvu']=0;
                        
                    if($this->data['Nvngay'][$i][$j][1]!=null)
                        $record['Nvngay']['chitieu']=$this->data['Nvngay'][$i][$j][1];
                    else
                        $record['Nvngay']['chitieu']=0;
                     
                    if($this->data['Nvngay'][$i][$j][2]!=null)
                        $record['Nvngay']['ketqua']=$this->data['Nvngay'][$i][$j][2];
                    else
                        $record['Nvngay']['ketqua']=0;
                    
                    if($this->data['Nvngay'][$i][$j][3]!=null)
                         $record['Nvngay']['tyle']=$this->data['Nvngay'][$i][$j][3];
                    else
                        $record['Nvngay']['tyle']=0;
                    
                    $record['Nvngay']['ngayth']=$j;
                    if(!$this->Nvngay->saveAll($record)){
                         $this->Session->setFlash(__('Lưu Thất Bại', true));
                         //$this->redirect(array('action' => 'kq',$kehoach));
                        
                                              
             		}
                    
            }
      
        $this->Session->setFlash(__('Đã lưu thành công', true));
        
        
        //kiem tra neu la ca nhan nhan vien nhap thi chuyen lai trang hien ca nhan
        //neu la nhan vien thi nhay trang hien
        if(empty($idnhanvien))
            $this->redirect(array('action' => 'hien',$kehoach));
        else
            $this->redirect(array('action' => 'hien',$kehoach,$this->data['Nvngay']['nv'][0]));
    }
    
    /** luu1: dung de edit (da luu it nhat 1 lan)
     * @param kehoach   id of kehoach which needs to save
     * */
     function luu1 ($kehoach=1,$idnhanvien=null){
     //   $colnum = $this->data['Nvngay']['socot'];
     
       
      //                  $this->Nvngay->query('START TRANSACTION;');
        $this->Nvngay->query('delete from nvngays where kehoach_id='.$kehoach);
       
                    
                    
		$rownum = $this->data['Nvngay']['sodong'];
        
        for($i=0;$i<$rownum;$i++)
            for($j=0;$j<7;$j++){
              $record['Nvngay']['kehoach_id']=$kehoach;
                    $record['Nvngay']['nhanvien_id']=$this->data['Nvngay']['nv'][$i];
                    if($this->data['Nvngay'][$i][$j][0]!=null)
                        $record['Nvngay']['nhiemvu']=$this->data['Nvngay'][$i][$j][0];
                    else
                        $record['Nvngay']['nhiemvu']=0;
                        
                    if($this->data['Nvngay'][$i][$j][1]!=null)
                        $record['Nvngay']['chitieu']=$this->data['Nvngay'][$i][$j][1];
                    else
                        $record['Nvngay']['chitieu']=0;
                     
                    if($this->data['Nvngay'][$i][$j][2]!=null)
                        $record['Nvngay']['ketqua']=$this->data['Nvngay'][$i][$j][2];
                    else
                        $record['Nvngay']['ketqua']=0;
                    
                    if($this->data['Nvngay'][$i][$j][3]!=null)
                         $record['Nvngay']['tyle']=$this->data['Nvngay'][$i][$j][3];
                    else
                        $record['Nvngay']['tyle']=0;
                    
                    $record['Nvngay']['ngayth']=$j;
                    if(!$this->Nvngay->saveAll($record)){
                         $this->Session->setFlash(__('Lưu Thất Bại', true));
                         //$this->redirect(array('action' => 'kq',$kehoach));
                        
        //                      $this->Nvngay->query('ROLL BACK;');
                    
                                              
             		}
                    
            }
     
        //$this->Nvngay->query('COMMIT;');
     
        $this->Session->setFlash(__('Đã lưu thành công', true));
	
    
     //kiem tra neu la ca nhan nhan vien nhap thi chuyen lai trang hien ca nhan
         if(empty($idnhanvien))
            $this->redirect(array('action' => 'hien',$kehoach));
        else
            $this->redirect(array('action' => 'hien',$kehoach,$this->data['Nvngay']['nv'][0]));          
    }
    function hien($kehoach=1,$idnhanvien=null){
       
       $kh=$this->Nvngay->Kehoach->find('first',array('conditions'=>array('Kehoach.id'=>$kehoach)));
       $this->set(compact('kh'));
       
        $this->Nvngay->recursive = -1;
        if(!empty($idnhanvien)){
            $dulieu=$this->Nvngay->find('all',array(
                'conditions'=>array('kehoach_id'=>$kehoach,'nhanvien_id'=>$idnhanvien),
                'order'=>array('nhanvien_id'=>'asc','id'=>'asc','ngayth'=>'asc')));
                    
            $nhanviens=$this->Tempnhiemvunv->find('all',array(
                'fields'=>array('id','Nhanvien.manv','Nhanvien.tennv','Tempnhiemvunv.kehoach_id',
                                        'Tempnhiemvunv.nhanvien_id'),
                'order'=>array('nhanvien_id'=>'asc'),
                'conditions'=>array('kehoach_id'=>$kehoach,'nhanvien_id'=>$idnhanvien)));
     //   print_r($dulieu);
        }  
        else{
            $dulieu=$this->Nvngay->find('all',array('conditions'=>array('kehoach_id'=>$kehoach),
                    'order'=>array('nhanvien_id'=>'asc','id'=>'asc','ngayth'=>'asc')));
                    
            $nhanviens=$this->Tempnhiemvunv->find('all',array(
                'fields'=>array('id','Nhanvien.manv','Nhanvien.tennv','Tempnhiemvunv.kehoach_id',
                                        'Tempnhiemvunv.nhanvien_id'),
                'order'=>array('nhanvien_id'=>'asc'),'conditions'=>array('kehoach_id'=>$kehoach)));
        }
        
            $this->set(compact('dulieu','nhanviens','kehoach','idnhanvien'));
        
        
    }
    /** sua dung de luu cong viec cua nhan vien o lan thu 2 tro di (dung de sua)
     * 
     * @param kehoach       idkehoach can luu cong viec
     * @param idnhanvien    dung trong truong hop ca nhan vien tu luu
     * 
     * @return              Tro ve "hien" sau khi nhap xong de ng ta kiem tra lai
     * */
    function sua($kehoach=1){
       
        $kh=$this->Nvngay->Kehoach->find('first',array('conditions'=>array('Kehoach.id'=>$kehoach)));
         //$this->set(compact('kh'));
          
        $this->Nvngay->recursive = -1;
        $dulieu=$this->Nvngay->find('all',array(
                    'conditions'=>array('kehoach_id'=>$kehoach),
                    'order'=>array('nhanvien_id'=>'asc','id'=>'asc','ngayth'=>'asc')));
        
        //neu chua co du lieu nao thi chuyen qua trang nhap lan dau tien
        if(count($dulieu)<1)
       	    $this->redirect(array('action' => 'kq',$kehoach));
            
                    
        $nhanviens=$this->Tempnhiemvunv->find('all',array(
                    'fields'=>array('id','Nhanvien.manv','Nhanvien.tennv','Tempnhiemvunv.kehoach_id',
                                        'Tempnhiemvunv.nhanvien_id'),
                    'order'=>array('nhanvien_id'=>'asc'),'conditions'=>array('kehoach_id'=>$kehoach)));
        
              
        //print_r($kehoach);
      
        $this->set(compact('dulieu','nhanviens','kehoach','idnhanvien','kh'));
        
    }
    
    /** suanv dung de luu cong viec cua nhan vien o lan thu 2 tro di (dung de sua)
     * 
     * @param kehoach       idkehoach can luu cong viec
     * @param idnhanvien    dung trong truong hop ca nhan vien tu luu
     * 
     * @return              Tro ve "hien" sau khi nhap xong de ng ta kiem tra lai
     * */
    function suanv($kehoach=1,$idnhanvien=null){
        
        $kh=$this->Nvngay->Kehoach->find('first',array('conditions'=>array('Kehoach.id'=>$kehoach)));
         $this->set(compact('kh'));
         
         
        $this->Nvngay->recursive = -1;
        $dulieu=$this->Nvngay->find('all',array(
                    'conditions'=>array('kehoach_id'=>$kehoach,'nhanvien_id'=>$idnhanvien),
                    'order'=>array('nhanvien_id'=>'asc','id'=>'asc','ngayth'=>'asc')));
                    
        
        if(count($dulieu)<1){
        	$this->Session->setFlash(__('Công việc của bạn chưa được giao', true));
       	    $this->redirect(array('controller'=>'kehoaches','action' => 'index'));
        }
        
        
        $nhanviens=$this->Tempnhiemvunv->find('all',array(
                    'fields'=>array('id','Nhanvien.manv','Nhanvien.tennv','Tempnhiemvunv.kehoach_id',
                                        'Tempnhiemvunv.nhanvien_id'),
                    'order'=>array('nhanvien_id'=>'asc'),
                    'conditions'=>array('kehoach_id'=>$kehoach,'nhanvien_id'=>$idnhanvien)));
            
                 
     //   print_r($dulieu);
      
        $this->set(compact('dulieu','nhanviens','kehoach','idnhanvien'));
        
    }
    /**
     * addnv de them 1 task moi cho 1 nhan vien, khi them 1 task moi thi dong thoi
     * luu nhan vien nay vao bang Tempnhiemvunvs de khi hien tro lai thi co 1 dong 
     * danh cho nhan vien nay
     * @param   kehoach
     * @param   nhanvien       id of nhanvien which needs to add
     * 
     * @return  Tro lai khung nhap task cho nhanvien
     * */
    function addnv($kehoach=1,$nhanvien=1){
        $nv=$this->Tempnhiemvunv->query('insert into Tempnhiemvunvs(kehoach_id,nhanvien_id) 
                    values ('.$kehoach.','.$nhanvien.')');
        
        $this->redirect(array('action' => 'kq',$kehoach));
        
        /** Khi phan quyen xong them 1 ham if de dieu huong cai nay,
         * Role NV thi se chay redirect phia duoi
         * Role truong TT, CN thi chay o tren
         * */
        //$this->redirect(array('action' => 'kq',$kehoach,$nhanvien));
    }
    
    /**
     * editnv de them 1 task moi cho 1 nhan vien (trong truong hop khung da co du lieu truoc do), 
     * khi them 1 task moi thi dong thoi
     * luu nhan vien nay vao bang Tempnhiemvunvs de khi hien tro lai thi co 1 dong 
     * danh cho nhan vien nay
     * @param   kehoach
     * @param   nhanvien       id of nhanvien which needs to add
     * 
     * @return  Tro lai khung nhap (sua)task cho nhanvien
     * */
    function editnv($kehoach=1,$nhanvien=1){
        $nv=$this->Tempnhiemvunv->query('insert into Tempnhiemvunvs(kehoach_id,nhanvien_id) 
                    values ('.$kehoach.','.$nhanvien.')');
        for($i=0;$i<7;$i++)
            $nv=$this->Tempnhiemvunv->query('insert into nvngays(kehoach_id,nhanvien_id,nhiemvu,ngayth) 
                    values ('.$kehoach.','.$nhanvien.',0,'.$i.')');
        $this->redirect(array('action' => 'sua',$kehoach));
    }
	function index() {
		$this->Nvngay->recursive = 0;
		$this->set('nvngays', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid nvngay', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('nvngay', $this->Nvngay->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Nvngay->create();
			if ($this->Nvngay->save($this->data)) {
				$this->Session->setFlash(__('The nvngay has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The nvngay could not be saved. Please, try again.', true));
			}
		}
		$kehoaches = $this->Nvngay->Kehoach->find('list');
		$nhanviens = $this->Nvngay->Nhanvien->find('list');
		$this->set(compact('kehoaches', 'nhanviens'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid nvngay', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Nvngay->save($this->data)) {
				$this->Session->setFlash(__('The nvngay has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The nvngay could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Nvngay->read(null, $id);
		}
		$kehoaches = $this->Nvngay->Kehoach->find('list');
		$nhanviens = $this->Nvngay->Nhanvien->find('list');
		$this->set(compact('kehoaches', 'nhanviens'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for nvngay', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Nvngay->delete($id)) {
			$this->Session->setFlash(__('Nvngay deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Nvngay was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>